﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using CrystalDecisions.CrystalReports.Engine;
using System.Drawing.Printing;


namespace GUI.MaVach
{
    public partial class frmQuanLy : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        public frmQuanLy()
        {
            InitializeComponent();
        }
        private Entities.ThongTinMaVach[] DanhSach;
        private Boolean CheckClose = false;
        #region gridview
        private void FixDatagridview()
        {
            try
            {
                dgvHangHoa.Columns[0].Visible = false;
                dgvHangHoa.Columns[1].Visible = false;
                dgvHangHoa.Columns[2].Visible = true;
                dgvHangHoa.Columns[3].Visible = true;
                dgvHangHoa.Columns[4].Visible = false;
                dgvHangHoa.Columns[5].Visible = false;
                dgvHangHoa.Columns[6].Visible = false;
                dgvHangHoa.Columns[7].Visible = true;

                dgvHangHoa.Columns[2].HeaderText = "Mã Hàng Hóa";
                dgvHangHoa.Columns[3].HeaderText = "Tên Hàng Hóa";
                dgvHangHoa.Columns[7].HeaderText = "Số lượng in";

                dgvHangHoa.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dgvHangHoa.Columns[2].Width = 100;
                dgvHangHoa.Columns[3].Width = 200;
                dgvHangHoa.Columns[7].Width = 100;
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        #region select
        private void GetHangHoa(string mahanghoa)
        {
            try
            {
                if (this.search == null)
                { return; }
                for (int i = 0; i < this.search.Length; i++)
                {
                    if (this.search[i].MaHangHoa.ToUpper() == mahanghoa.ToUpper())
                    {
                        txtMaHangHoa.Text = this.search[i].MaHangHoa;
                        txtTenHangHoa.Text = this.search[i].TenHangHoa;
                        txtSoLuongIn.Text = "1";
                        break;
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        private void SelectData()
        {
            try
            {
                Entities.ThongTinMaVach nv = new Entities.ThongTinMaVach("Select");
                nv.HanhDong = "0";
                // Common.StaticVariable.InterfaceObject cham' 1 cai se lay dc cac ham tu sql ma minh da tao trong interface
                DanhSach = Common.StaticVariable.InterfaceObject.ThongTinMaVachHangHoa(nv);
                dgvHangHoa.DataSource = DanhSach;
                //hien thi lai cai gidview
               // this.HienThiListview();
                this.SelectGoiHang();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                this.DanhSach = null;
            }
        }
        Entities.GoiHang[] goihang;
        private ArrayList list = new ArrayList();
        public void SelectGoiHang()
        {
            try
            {
                Entities.GoiHang nv = new Entities.GoiHang();
                nv.HanhDong = "1";
                nv.MaGoiHang = "";
                goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang(nv);
                try
                {
                    for (int j = 0; j < goihang.Length; j++)
                    {
                        if (goihang[j].Deleted == false)
                        {
                            Entities.ThongTinMaVach row = new Entities.ThongTinMaVach();
                            row.MaHangHoa = goihang[j].MaGoiHang;
                            row.TenHangHoa = goihang[j].TenGoiHang;
                            list.Add(row);
                        }
                    }
                }
                catch { }
                try
                {
                    for (int i = 0; i < this.DanhSach.Length; i++)
                    {
                        list.Add(this.DanhSach[i]);
                    }
                }
                catch { }
                int k = list.Count;
                if (k <= 0)
                {
                    this.DanhSach = null;
                }
                else
                {
                    this.DanhSach = new Entities.ThongTinMaVach[k];
                    for (int i = 0; i < k; i++)
                    {
                        this.DanhSach[i] = (Entities.ThongTinMaVach)list[i];
                    }
                }
            }
            catch { }
        }
        #endregion

        #region barcode
        private void fix()
        {
            if (checkBox1.Checked == true)
            {
                btnindanhsach.Enabled = true;
                button1.Enabled = false;
            }
            else
            { btnindanhsach.Enabled = false; button1.Enabled = true; }
        }

        private BarcodeLib.Barcode b = new BarcodeLib.Barcode();
        private void btnForeColor_Click(object sender, EventArgs e)
        {
            try
            {
                using (ColorDialog cdialog = new ColorDialog())
                {
                    cdialog.AnyColor = true;
                    if (cdialog.ShowDialog() == DialogResult.OK)
                    {
                        this.b.ForeColor = cdialog.Color;
                        this.btnForeColor.BackColor = this.b.ForeColor;
                    }
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            try
            {
                try
                {
                    this.SelectData();
                }
                catch (Exception)
                {
                }
                chkGenerateLabel.Checked = false;
                cbxLoaigiay.Items.Clear();
                this.cbxLoaigiay.Items.AddRange(new object[] { "Loại A5", "Loại 110", "Loại A4" });
                cbxLoaigiay.SelectedIndex = 0;
                //cbxLoaigiay.Enabled = false;
                btnLoadImage.Visible = false;
                Bitmap temp = new Bitmap(1, 1);
                temp.SetPixel(0, 0, this.BackColor);
                barcode.Image = (Image)temp;
                this.cbEncodeType.SelectedIndex = 0;
                this.cbBarcodeAlign.SelectedIndex = 0;
                this.cbLabelLocation.SelectedIndex = 0;
                this.cbRotateFlip.DataSource = System.Enum.GetNames(typeof(RotateFlipType));
                int i = 0;
                foreach (object o in cbRotateFlip.Items)
                {
                    if (o.ToString().Trim().ToLower() == "rotatenoneflipnone")
                        break;
                    i++;
                }
                this.cbRotateFlip.SelectedIndex = i;
                this.btnBackColor.BackColor = this.b.BackColor;
                this.btnForeColor.BackColor = this.b.ForeColor;
                checkBox1.Checked = false;
                fix();
                Entities.ThongTinMaVach[] mv = new Entities.ThongTinMaVach[0];
                dgvHangHoa.DataSource = mv;
                FixDatagridview();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                if (dgvHangHoa.RowCount <= 0)
                { btnindanhsach.Enabled = false; checkBox1.Enabled = false; }
            }
        }

        private Entities.ThongTinMaVach[] listview;
        private string MaHangHoa = string.Empty;
        private void Edits(string MaHangHoa)
        {
            ArrayList li = new ArrayList();
            try
            {
                if (listview == null)
                {
                    li = null;
                }
                else
                {
                    for (int i = 0; i < listview.Length; i++)
                    {
                        if (listview[i].MaHangHoa != MaHangHoa)
                        {
                            li.Add(listview[i]);
                        }
                        if (listview[i].MaHangHoa == MaHangHoa)
                        {
                            txtMaHangHoa.Text = listview[i].MaHangHoa;
                            txtTenHangHoa.Text = listview[i].TenHangHoa;
                            txtSoLuongIn.Text = listview[i].GhiChu;
                            txtSoLuongIn.Focus();
                        }
                    }
                }
                int k = li.Count;
                if (k <= 0)
                {
                    listview = new Entities.ThongTinMaVach[0];
                }
                else
                {
                    listview = new Entities.ThongTinMaVach[k];
                    for (int i = 0; i < k; i++)
                    {
                        listview[i] = (Entities.ThongTinMaVach)li[i];
                    }
                }
                dgvHangHoa.DataSource = listview;
                FixDatagridview();
            }
            catch (Exception)
            {

            }
        }
        private void dgvHangHoa_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.Edits(this.MaHangHoa);
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); }
        }

        #endregion

        #region thoát
        private void btndong_Click(object sender, EventArgs e)
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.CheckClose = true;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #region vẽ mã vạch
        private Image img;
        private void VeMaVach(string code)
        {
            try
            {
                errorProvider1.Clear();
                int W = Convert.ToInt32(this.txtWidth.Text.Trim());
                int H = Convert.ToInt32(this.txtHeight.Text.Trim());
                BarcodeLib.AlignmentPositions Align = BarcodeLib.AlignmentPositions.CENTER;
                switch (cbBarcodeAlign.SelectedItem.ToString().Trim().ToLower())
                {
                    case "left": Align = BarcodeLib.AlignmentPositions.LEFT; break;
                    case "right": Align = BarcodeLib.AlignmentPositions.RIGHT; break;
                    default: Align = BarcodeLib.AlignmentPositions.CENTER; break;
                }

                BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED;
                switch (cbEncodeType.SelectedItem.ToString().Trim())
                {
                    case "UPC-A": type = BarcodeLib.TYPE.UPCA; break;
                    case "UPC-E": type = BarcodeLib.TYPE.UPCE; break;
                    case "UPC 2 Digit Ext.": type = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_2DIGIT; break;
                    case "UPC 5 Digit Ext.": type = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_5DIGIT; break;
                    case "EAN-13": type = BarcodeLib.TYPE.EAN13; break;
                    case "JAN-13": type = BarcodeLib.TYPE.JAN13; break;
                    case "EAN-8": type = BarcodeLib.TYPE.EAN8; break;
                    case "ITF-14": type = BarcodeLib.TYPE.ITF14; break;
                    case "Codabar": type = BarcodeLib.TYPE.Codabar; break;
                    case "PostNet": type = BarcodeLib.TYPE.PostNet; break;
                    case "Bookland/ISBN": type = BarcodeLib.TYPE.BOOKLAND; break;
                    case "Code 11": type = BarcodeLib.TYPE.CODE11; break;
                    case "Code 39": type = BarcodeLib.TYPE.CODE39; break;
                    case "Code 39 Extended": type = BarcodeLib.TYPE.CODE39Extended; break;
                    case "Code 93": type = BarcodeLib.TYPE.CODE93; break;
                    case "LOGMARS": type = BarcodeLib.TYPE.LOGMARS; break;
                    case "MSI": type = BarcodeLib.TYPE.MSI_Mod10; break;
                    case "Interleaved 2 of 5": type = BarcodeLib.TYPE.Interleaved2of5; break;
                    case "Standard 2 of 5": type = BarcodeLib.TYPE.Standard2of5; break;
                    case "Code 128": type = BarcodeLib.TYPE.CODE128; break;
                    case "Code 128-A": type = BarcodeLib.TYPE.CODE128A; break;
                    case "Code 128-B": type = BarcodeLib.TYPE.CODE128B; break;
                    case "Code 128-C": type = BarcodeLib.TYPE.CODE128C; break;
                    case "Telepen": type = BarcodeLib.TYPE.TELEPEN; break;
                    default: MessageBox.Show("Please specify the encoding type."); break;
                }
                try
                {
                    if (type != BarcodeLib.TYPE.UNSPECIFIED)
                    {
                        barcode.Image = null;
                        b.IncludeLabel = this.chkGenerateLabel.Checked;
                        b.Alignment = Align;
                        b.RotateFlipType = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), this.cbRotateFlip.SelectedItem.ToString(), true);
                        switch (this.cbLabelLocation.SelectedItem.ToString().Trim().ToUpper())
                        {
                            case "BOTTOMLEFT": b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMLEFT; break;
                            case "BOTTOMRIGHT": b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMRIGHT; break;
                            case "TOPCENTER": b.LabelPosition = BarcodeLib.LabelPositions.TOPCENTER; break;
                            case "TOPLEFT": b.LabelPosition = BarcodeLib.LabelPositions.TOPLEFT; break;
                            case "TOPRIGHT": b.LabelPosition = BarcodeLib.LabelPositions.TOPRIGHT; break;
                            default: b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER; break;
                        }//switch

                        //===== Encoding performed here =====
                        img = b.Encode(type, code, this.btnForeColor.BackColor, this.btnBackColor.BackColor, W, H);
                        barcode.Image = img;
                        //===================================
                        this.lblEncodingTime.Text = "(" + Math.Round(b.EncodingTime, 0, MidpointRounding.AwayFromZero).ToString() + "ms)";
                        txtEncoded.Text = b.EncodedValue;
                    }
                    barcode.Width = barcode.Image.Width;
                    barcode.Height = barcode.Image.Height;
                    barcode.Location = new Point((this.groupBox2.Location.X + this.groupBox2.Width / 2) - barcode.Width / 2, (this.groupBox2.Location.Y + this.groupBox2.Height / 2) - barcode.Height / 2);
                }
                catch (Exception ex)
                {
                    string s = ex.Message.ToString();
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        #region btn
        private void btnEncode_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtData.Text != "123456")
                {
                    VeMaVach(txtData.Text.Trim());
                }
                else { MessageBox.Show("Nhập mã cần hiển thị mã vạch"); }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPG (*.jpg)|*.jpg|PNG (*.png)|*.png|TIFF (*.tif)|*.tif";
                sfd.FilterIndex = 2;
                sfd.AddExtension = true;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    BarcodeLib.SaveTypes savetype = BarcodeLib.SaveTypes.UNSPECIFIED;
                    switch (sfd.FilterIndex)
                    {
                        case 1: /* BMP */  savetype = BarcodeLib.SaveTypes.BMP; break;
                        case 2: /* GIF */  savetype = BarcodeLib.SaveTypes.GIF; break;
                        case 3: /* JPG */  savetype = BarcodeLib.SaveTypes.JPG; break;
                        case 4: /* PNG */  savetype = BarcodeLib.SaveTypes.PNG; break;
                        case 5: /* TIFF */ savetype = BarcodeLib.SaveTypes.TIFF; break;
                        default: break;
                    }
                    b.SaveImage(sfd.FileName, savetype);
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void btnBackColor_Click(object sender, EventArgs e)
        {
            try
            {
                using (ColorDialog cdialog = new ColorDialog())
                {
                    cdialog.AnyColor = true;
                    if (cdialog.ShowDialog() == DialogResult.OK)
                    {
                        this.b.BackColor = cdialog.Color;
                        this.btnBackColor.BackColor = this.b.BackColor;
                    }
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void btnLoadImage_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    barcode.BackgroundImage = null;
                    barcode.BackgroundImage = Image.FromFile(openFileDialog1.FileName);
                    barcode.BackgroundImageLayout = ImageLayout.Center;
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        #region lấy hàng hóa theo mã
        private void LayHangHoaTheoMa(string MaHang)
        {
            try
            {
                Entities.HienThi_ChiTiet_DonDatHang ktm = new Entities.HienThi_ChiTiet_DonDatHang();
                ktm.MaHangHoa = "";
                Entities.HienThi_ChiTiet_DonDatHang tra = new Entities.HienThi_ChiTiet_DonDatHang();
                tra = Common.StaticVariable.InterfaceObject.HienThi_ChiTiet_DonDatHang(ktm);
               // dgvHangHoa.DataSource = DanhSach;
                if (tra.MaHangHoa == null || tra == null)
                {
                   HangHoa.frmXuLy frm = new HangHoa.frmXuLy();
                    frm.ShowDialog();
                    txtData.Text = Luu.GiaTriCanLuu.mahanghoa;
                    LayHangHoaTheoMa(txtData.Text);
                }
                else
                {
                    txtData.Text = tra.MaHangHoa;
                    txtTen.Text = tra.TenHangHoa;
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); }
        }
        #endregion

        #region barcode a5,4,110
        private void btnindanhsach_Click(object sender, EventArgs e)
        {
            try
            {
                //quyen
                //if (!Common.Utilities.User.Administrator && !CheckQuyen(this.Name, 4))
                //{ return; }

                if (checkBox1.Checked == true)
                {
                    if (dgvHangHoa.RowCount > 0)
                    {
                        this.Enabled = false;
                        DanhSachMaVach();
                        this.Enabled = true;
                        string loaiGiay = cbxLoaigiay.SelectedItem.ToString();
                        switch (loaiGiay)
                        {
                            case "Loại A5":
                                //in theo 2 cot
                                Entities.Barcode_A5[] mangin2 = HaiCot();
                                if (mangin2.Length > 0)
                                {
                                    if (checkXemIn.Checked == false)
                                    {
                                        BaoCao.Report.rptBarcodeHaiCot report = new BaoCao.Report.rptBarcodeHaiCot();
                                        report.SetDataSource(mangin2);
                                        report.PrintToPrinter(1, true, 0, 999);
                                    }
                                    else
                                    {

                                        BaoCao.frmBaoCaoBarcode frm = new BaoCao.frmBaoCaoBarcode(mangin2, "Loại A5", checkXemIn.Checked);
                                        frm.ShowDialog();
                                        danhsach = null;
                                    }
                                }
                                else
                                { MessageBox.Show("Chọn hàng hóa mới có thể in"); }
                                break;
                            case "Loại 110":
                                // in theo 3 cot
                                Entities.Barcode_110[] mangin3 = BaCot();
                                if (mangin3.Length > 0)
                                {
                                    if (checkXemIn.Checked == false)
                                    {
                                        BaoCao.Report.rptBarcodeBaCot report = new BaoCao.Report.rptBarcodeBaCot();
                                        report.SetDataSource(mangin3);
                                        report.PrintToPrinter(1, true, 0, 999);
                                    }
                                    else
                                    {
                                        BaoCao.frmBaoCaoBarcode frm = new BaoCao.frmBaoCaoBarcode(mangin3, "Loại 110", checkXemIn.Checked);
                                        frm.ShowDialog();
                                        danhsach = null;
                                    }
                                }
                                else
                                { MessageBox.Show("Chọn hàng hóa mới có thể in"); }
                                break;
                            case "Loại A4":
                                // in theo 5 cot
                                Entities.Barcode_A4[] mangin5 = NamCot();
                                if (mangin5.Length > 0)
                                {
                                    if (checkXemIn.Checked == false)
                                    {
                                        BaoCao.Report.rptBarcodeNamCot report = new BaoCao.Report.rptBarcodeNamCot();
                                        report.SetDataSource(mangin5);
                                        report.PrintToPrinter(1, true, 0, 999);
                                    }
                                    else
                                    {
                                        BaoCao.frmBaoCaoBarcode frm = new BaoCao.frmBaoCaoBarcode(mangin5, "Loại A4", checkXemIn.Checked);
                                        frm.ShowDialog();
                                        danhsach = null;
                                    }
                                }
                                else
                                { MessageBox.Show("Chọn hàng hóa mới có thể in"); }
                                break;
                            default:
                                danhsach = null;
                                break;
                        }
                    }
                    else
                    { MessageBox.Show("Không có hàng hóa nào để in mã vạch"); }
                }
                else
                { MessageBox.Show("Bạn đang chọn in một mã vạch"); checkBox1.Focus(); }

                dgvHangHoa.DataSource = new Entities.ThongTinMaVach[0];
            }
            catch (Exception ex)
            { string s = ex.Message; danhsach = null; }
        }
        

        private int soCot = 1;
        private Entities.InMaVach[] danhsach;
        private void DanhSachMaVachMotCot()
        {
            try
            {
                danhsach = null;
                ArrayList arr = new ArrayList();
                int soluong = 1;
                for (int k = 0; k < soluong; k++)
                {
                    string tenhang = txtTen.Text;
                    string mahang = txtData.Text;
                    Entities.InMaVach row = new Entities.InMaVach();
                    row.MaHangHoa = mahang;
                    row.TenHangHoa = tenhang;
                    VeMaVach(mahang);
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    barcode.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    Byte[] manganh = new Byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(manganh, 0, Convert.ToInt32(ms.Length));
                    row.Anh = manganh;
                    arr.Add(row);
                    mahang = null;
                    tenhang = null;
                    img = null;
                }
                int n = arr.Count;
                danhsach = new Entities.InMaVach[n];
                if (n == 0) { danhsach = null; }
                danhsach = new Entities.InMaVach[n];
                for (int x = 0; x < n; x++)
                {
                    danhsach[x] = (Entities.InMaVach)arr[x];
                }
            }
            catch (Exception ex)
            { string s = ex.Message; this.Enabled = true; danhsach = null; }
        }
        private void DanhSachMaVach()
        {
            try
            {
                if (dgvHangHoa.Rows.Count > 0)
                {
                    ArrayList arr = new ArrayList();
                    for (int k = 0; k < dgvHangHoa.Rows.Count; k++)
                    {
                        string na = dgvHangHoa[3, k].Value.ToString();
                        int u = 0;
                        try
                        { u = int.Parse(dgvHangHoa[7, k].Value.ToString()); }
                        catch (Exception)
                        { MessageBox.Show("Số lượng hàng :" + na + " nhập không đúng định dạng hoặc số lượng in quá lớn"); danhsach = null; return; }
                        if (u > 0)
                        {
                            if (u.ToString().Length <= 3 && u.ToString().Length > 0)
                            {
                                for (int h = 0; h < u; h++)
                                {
                                    img = null;
                                    Entities.InMaVach row = new Entities.InMaVach();
                                    string ma = dgvHangHoa.Rows[k].Cells[2].Value.ToString();
                                    string ten = dgvHangHoa.Rows[k].Cells[3].Value.ToString();
                                    txtData.Text = ma;
                                    txtTen.Text = ten;
                                    row.MaHangHoa = ma;
                                    row.TenHangHoa = ten;
                                    VeMaVach(ma);
                                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                                    barcode.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                                    Byte[] manganh = new Byte[ms.Length];
                                    ms.Position = 0;
                                    ms.Read(manganh, 0, Convert.ToInt32(ms.Length));
                                    row.Anh = manganh;
                                    arr.Add(row);
                                    ma = null;
                                    ten = null;
                                    img = null;
                                }
                            }
                            else
                            { MessageBox.Show("Số lượng hàng :" + na + " phải nhỏ hơn 9 kí tự"); danhsach = null; return; }
                        }
                        else
                        {
                            MessageBox.Show("Số lượng hàng :" + na + " phải lớn hơn 0"); danhsach = null; return;
                        }
                    }
                    int n = arr.Count;
                    danhsach = new Entities.InMaVach[n];
                    if (n == 0) { danhsach = null; }
                    danhsach = new Entities.InMaVach[n];
                    for (int x = 0; x < n; x++)
                    {
                        danhsach[x] = (Entities.InMaVach)arr[x];
                    }
                }
            }
            catch (Exception ex)
            { string s = ex.Message; this.Enabled = true; danhsach = null; }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //if (!Common.Utilities.User.Administrator && !CheckQuyen(this.Name, 4))
                //{ return; }
                if (checkBox1.Checked == false)
                {
                    if (txtData.Text != "123456" && txtData.Text != "")
                    {
                        ten = "";
                        LayHangHoaTheoMa(txtData.Text);
                        this.Enabled = false;
                        DanhSachMaVachMotCot();
                        this.Enabled = true;
                        if (danhsach.Length > 0)
                        {
                            if (checkXemIn.Checked == false)
                            {
                                BaoCao.Report.rptBarcode report = new BaoCao.Report.rptBarcode();
                                report.SetDataSource(danhsach);
                                report.PrintToPrinter(1, true, 0, 999);
                            }
                            else
                            {
                                BaoCao.frmBaoCaoBarcode frm = new BaoCao.frmBaoCaoBarcode(danhsach, "MotCot", checkXemIn.Checked);
                                frm.ShowDialog();
                                danhsach = null;
                            }
                        }
                        else
                        { MessageBox.Show("Chọn hàng hóa mới có thể in"); }
                    }
                    else
                    { MessageBox.Show("Nhập chưa nhập mã cần in"); txtData.Focus(); txtData.Text = ""; }
                }
                else
                { MessageBox.Show("Số lượng in phải lớn hơn 0"); }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        private Entities.Barcode_A5[] HaiCot()
        {
            Entities.Barcode_A5[] tra = null;
            try
            {
                int count = 0;
                int soDu = danhsach.Length % soCot;
                ArrayList list = new ArrayList();
                if (danhsach.Length >= soCot)
                {
                    count = ((danhsach.Length - soDu) / soCot);
                    int g = 0;
                    for (int i = 0; i < count; i++)
                    {
                        for (int k = 0; k < soCot; k++)
                        {
                            if (k == 0)
                            {
                                Entities.Barcode_A5 banghi = new Entities.Barcode_A5();
                                banghi.MaHangHoaMot = danhsach[k + g].MaHangHoa;
                                banghi.TenHangHoaMot = danhsach[k + g].TenHangHoa;
                                banghi.AnhMot = danhsach[k + g].Anh;
                                banghi.MaHangHoaHai = danhsach[(1 + k) + g].MaHangHoa;
                                banghi.TenHangHoaHai = danhsach[(1 + k) + g].TenHangHoa;
                                banghi.AnhHai = danhsach[(1 + k) + g].Anh;
                                list.Add(banghi);
                            }
                            else
                            { continue; }
                        }
                        g += soCot;
                    }
                    switch (soDu)
                    {
                        case 0:
                            break;
                        case 1:
                            Entities.Barcode_A5 banghi2 = new Entities.Barcode_A5();
                            banghi2.MaHangHoaMot = danhsach[danhsach.Length - 1].MaHangHoa;
                            banghi2.TenHangHoaMot = danhsach[danhsach.Length - 1].TenHangHoa;
                            banghi2.AnhMot = danhsach[danhsach.Length - 1].Anh;
                            banghi2.MaHangHoaHai = null;
                            banghi2.TenHangHoaHai = null;
                            banghi2.AnhHai = null;
                            list.Add(banghi2);
                            break;
                        default:
                            tra = null;
                            break;
                    }
                }
                else
                {
                    if (danhsach.Length == 1)
                    {
                        Entities.Barcode_A5 banghi = new Entities.Barcode_A5();
                        banghi.MaHangHoaMot = danhsach[0].MaHangHoa;
                        banghi.TenHangHoaMot = danhsach[0].TenHangHoa;
                        banghi.AnhMot = danhsach[0].Anh;
                        banghi.MaHangHoaHai = null;
                        banghi.TenHangHoaHai = null;
                        banghi.AnhHai = null;
                        list.Add(banghi);
                    }
                    else
                    { tra = null; }
                }
                int n = list.Count;
                if (n == 0) { tra = null; }
                tra = new Entities.Barcode_A5[n];
                for (int i = 0; i < n; i++)
                {
                    tra[i] = (Entities.Barcode_A5)list[i];
                }
            }
            catch (Exception ex)
            { string s = ex.Message; tra = null; danhsach = null; }
            return tra;
        }
        private Entities.Barcode_110[] BaCot()
        {
            Entities.Barcode_110[] tra = null;
            try
            {
                ArrayList list = new ArrayList();
                int count = 0;
                int soDu = danhsach.Length % soCot;
                if (danhsach.Length >= soCot)
                {
                    count = ((danhsach.Length - soDu) / soCot);
                    int g = 0;
                    for (int i = 0; i < count; i++)
                    {
                        for (int k = 0; k < soCot; k++)
                        {
                            if (k == 0)
                            {
                                Entities.Barcode_110 banghi = new Entities.Barcode_110();
                                banghi.MaHangHoaMot = danhsach[k + g].MaHangHoa;
                                banghi.TenHangHoaMot = danhsach[k + g].TenHangHoa;
                                banghi.AnhMot = danhsach[k + g].Anh;

                                banghi.MaHangHoaHai = danhsach[(1 + k) + g].MaHangHoa;
                                banghi.TenHangHoaHai = danhsach[(1 + k) + g].TenHangHoa;
                                banghi.AnhHai = danhsach[(1 + k) + g].Anh;

                                banghi.MaHangHoaBa = danhsach[(2 + k) + g].MaHangHoa;
                                banghi.TenHangHoaBa = danhsach[(2 + k) + g].TenHangHoa;
                                banghi.AnhBa = danhsach[(2 + k) + g].Anh;
                                list.Add(banghi);
                            }
                            else
                            { continue; }
                        }
                        g += soCot;
                    }
                    switch (soDu)
                    {
                        case 0:
                            break;
                        case 1:
                            Entities.Barcode_110 banghi2 = new Entities.Barcode_110();
                            banghi2.MaHangHoaMot = danhsach[danhsach.Length - 1].MaHangHoa;
                            banghi2.TenHangHoaMot = danhsach[danhsach.Length - 1].TenHangHoa;
                            banghi2.AnhMot = danhsach[danhsach.Length - 1].Anh;
                            banghi2.MaHangHoaHai = null;
                            banghi2.TenHangHoaHai = null;
                            banghi2.AnhHai = null;
                            banghi2.MaHangHoaHai = null;
                            banghi2.TenHangHoaHai = null;
                            banghi2.AnhHai = null;
                            list.Add(banghi2);
                            break;
                        case 2:
                            Entities.Barcode_110 banghi3 = new Entities.Barcode_110();
                            banghi3.MaHangHoaMot = danhsach[danhsach.Length - 1].MaHangHoa;
                            banghi3.TenHangHoaMot = danhsach[danhsach.Length - 1].TenHangHoa;
                            banghi3.AnhMot = danhsach[danhsach.Length - 1].Anh;
                            banghi3.MaHangHoaHai = danhsach[danhsach.Length - 2].MaHangHoa;
                            banghi3.TenHangHoaHai = danhsach[danhsach.Length - 2].TenHangHoa;
                            banghi3.AnhHai = danhsach[danhsach.Length - 2].Anh;
                            banghi3.MaHangHoaBa = null;
                            banghi3.TenHangHoaBa = null;
                            banghi3.AnhBa = null;
                            list.Add(banghi3);
                            break;
                        default:
                            tra = null;
                            break;
                    }
                }
                else
                {
                    if (danhsach.Length == 1)
                    {
                        Entities.Barcode_110 banghi4 = new Entities.Barcode_110();
                        banghi4.MaHangHoaMot = danhsach[0].MaHangHoa;
                        banghi4.TenHangHoaMot = danhsach[0].TenHangHoa;
                        banghi4.AnhMot = danhsach[0].Anh;
                        banghi4.MaHangHoaHai = null;
                        banghi4.TenHangHoaHai = null;
                        banghi4.AnhHai = null;
                        banghi4.MaHangHoaBa = null;
                        banghi4.TenHangHoaBa = null;
                        banghi4.AnhBa = null;
                        list.Add(banghi4);
                    }
                    else
                    {
                        if (danhsach.Length == 2)
                        {
                            Entities.Barcode_110 banghi4 = new Entities.Barcode_110();
                            banghi4.MaHangHoaMot = danhsach[0].MaHangHoa;
                            banghi4.TenHangHoaMot = danhsach[0].TenHangHoa;
                            banghi4.AnhMot = danhsach[0].Anh;
                            banghi4.MaHangHoaHai = danhsach[1].MaHangHoa;
                            banghi4.TenHangHoaHai = danhsach[1].TenHangHoa;
                            banghi4.AnhHai = danhsach[1].Anh;
                            banghi4.MaHangHoaBa = null;
                            banghi4.TenHangHoaBa = null;
                            banghi4.AnhBa = null;
                            list.Add(banghi4);
                        }
                        else
                        {
                            tra = null;
                        }
                    }

                }
                int n = list.Count;
                if (n == 0) { tra = null; }
                tra = new Entities.Barcode_110[n];
                for (int i = 0; i < n; i++)
                {
                    tra[i] = (Entities.Barcode_110)list[i];
                }
            }
            catch (Exception ex)
            { string s = ex.Message; tra = null; danhsach = null; }
            return tra;
        }
        private Entities.Barcode_A4[] NamCot()
        {
            Entities.Barcode_A4[] tra = null;
            try
            {
                ArrayList list = new ArrayList();
                int count = 0;
                int soDu = danhsach.Length % soCot;
                if (danhsach.Length >= soCot)
                {
                    count = ((danhsach.Length - soDu) / soCot);
                    int g = 0;
                    for (int i = 0; i < count; i++)
                    {
                        for (int k = 0; k < soCot; k++)
                        {
                            if (k == 0)
                            {
                                Entities.Barcode_A4 banghi = new Entities.Barcode_A4();
                                banghi.MaHangHoaMot = danhsach[k + g].MaHangHoa;
                                banghi.TenHangHoaMot = danhsach[k + g].TenHangHoa;
                                banghi.AnhMot = danhsach[k + g].Anh;

                                banghi.MaHangHoaHai = danhsach[(1 + k) + g].MaHangHoa;
                                banghi.TenHangHoaHai = danhsach[(1 + k) + g].TenHangHoa;
                                banghi.AnhHai = danhsach[(1 + k) + g].Anh;

                                banghi.MaHangHoaBa = danhsach[(2 + k) + g].MaHangHoa;
                                banghi.TenHangHoaBa = danhsach[(2 + k) + g].TenHangHoa;
                                banghi.AnhBa = danhsach[(2 + k) + g].Anh;

                                banghi.MaHangHoaBon = danhsach[(3 + k) + g].MaHangHoa;
                                banghi.TenHangHoaBon = danhsach[(3 + k) + g].TenHangHoa;
                                banghi.AnhBon = danhsach[(3 + k) + g].Anh;

                                banghi.MaHangHoaNam = danhsach[(4 + k) + g].MaHangHoa;
                                banghi.TenHangHoaNam = danhsach[(4 + k) + g].TenHangHoa;
                                banghi.AnhNam = danhsach[(4 + k) + g].Anh;
                                list.Add(banghi);
                            }
                            else
                            { continue; }
                        }
                        g += soCot;
                    }
                    switch (soDu)
                    {
                        case 0:
                            break;
                        case 1:
                            Entities.Barcode_A4 banghi2 = new Entities.Barcode_A4();
                            banghi2.MaHangHoaMot = danhsach[danhsach.Length - 1].MaHangHoa;
                            banghi2.TenHangHoaMot = danhsach[danhsach.Length - 1].TenHangHoa;
                            banghi2.AnhMot = danhsach[danhsach.Length - 1].Anh;
                            banghi2.MaHangHoaHai = null;
                            banghi2.TenHangHoaHai = null;
                            banghi2.AnhHai = null;
                            banghi2.MaHangHoaBa = null;
                            banghi2.TenHangHoaBa = null;
                            banghi2.AnhBa = null;
                            banghi2.MaHangHoaBon = null;
                            banghi2.TenHangHoaBon = null;
                            banghi2.AnhBon = null;
                            banghi2.MaHangHoaNam = null;
                            banghi2.TenHangHoaNam = null;
                            banghi2.AnhNam = null;
                            list.Add(banghi2);
                            break;
                        case 2:
                            Entities.Barcode_A4 banghi3 = new Entities.Barcode_A4();
                            banghi3.MaHangHoaMot = danhsach[danhsach.Length - 1].MaHangHoa;
                            banghi3.TenHangHoaMot = danhsach[danhsach.Length - 1].TenHangHoa;
                            banghi3.AnhMot = danhsach[danhsach.Length - 1].Anh;
                            banghi3.MaHangHoaHai = danhsach[danhsach.Length - 2].MaHangHoa;
                            banghi3.TenHangHoaHai = danhsach[danhsach.Length - 2].TenHangHoa;
                            banghi3.AnhHai = danhsach[danhsach.Length - 2].Anh;
                            banghi3.MaHangHoaBa = null;
                            banghi3.TenHangHoaBa = null;
                            banghi3.AnhBa = null;
                            banghi3.MaHangHoaBon = null;
                            banghi3.TenHangHoaBon = null;
                            banghi3.AnhBon = null;
                            banghi3.MaHangHoaNam = null;
                            banghi3.TenHangHoaNam = null;
                            banghi3.AnhNam = null;
                            list.Add(banghi3);
                            break;
                        case 3:
                            Entities.Barcode_A4 banghi4 = new Entities.Barcode_A4();
                            banghi4.MaHangHoaMot = danhsach[danhsach.Length - 1].MaHangHoa;
                            banghi4.TenHangHoaMot = danhsach[danhsach.Length - 1].TenHangHoa;
                            banghi4.AnhMot = danhsach[danhsach.Length - 1].Anh;
                            banghi4.MaHangHoaHai = danhsach[danhsach.Length - 2].MaHangHoa;
                            banghi4.TenHangHoaHai = danhsach[danhsach.Length - 2].TenHangHoa;
                            banghi4.AnhHai = danhsach[danhsach.Length - 2].Anh;
                            banghi4.MaHangHoaBa = danhsach[danhsach.Length - 3].MaHangHoa;
                            banghi4.TenHangHoaBa = danhsach[danhsach.Length - 3].TenHangHoa;
                            banghi4.AnhBa = danhsach[danhsach.Length - 3].Anh;
                            banghi4.MaHangHoaBon = null;
                            banghi4.TenHangHoaBon = null;
                            banghi4.AnhBon = null;
                            banghi4.MaHangHoaNam = null;
                            banghi4.TenHangHoaNam = null;
                            banghi4.AnhNam = null;
                            list.Add(banghi4);
                            break;
                        case 4:
                            Entities.Barcode_A4 banghi5 = new Entities.Barcode_A4();
                            banghi5.MaHangHoaMot = danhsach[danhsach.Length - 1].MaHangHoa;
                            banghi5.TenHangHoaMot = danhsach[danhsach.Length - 1].TenHangHoa;
                            banghi5.AnhMot = danhsach[danhsach.Length - 1].Anh;
                            banghi5.MaHangHoaHai = danhsach[danhsach.Length - 2].MaHangHoa;
                            banghi5.TenHangHoaHai = danhsach[danhsach.Length - 2].TenHangHoa;
                            banghi5.AnhHai = danhsach[danhsach.Length - 2].Anh;
                            banghi5.MaHangHoaBa = danhsach[danhsach.Length - 3].MaHangHoa;
                            banghi5.TenHangHoaBa = danhsach[danhsach.Length - 3].TenHangHoa;
                            banghi5.AnhBa = danhsach[danhsach.Length - 3].Anh;
                            banghi5.MaHangHoaBon = danhsach[danhsach.Length - 4].MaHangHoa;
                            banghi5.TenHangHoaBon = danhsach[danhsach.Length - 4].TenHangHoa;
                            banghi5.AnhBon = danhsach[danhsach.Length - 4].Anh;
                            banghi5.MaHangHoaNam = null;
                            banghi5.TenHangHoaNam = null;
                            banghi5.AnhNam = null;
                            list.Add(banghi5);
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    if (danhsach.Length == 1)
                    {
                        Entities.Barcode_A4 banghi2 = new Entities.Barcode_A4();
                        banghi2.MaHangHoaMot = danhsach[danhsach.Length - 1].MaHangHoa;
                        banghi2.TenHangHoaMot = danhsach[danhsach.Length - 1].TenHangHoa;
                        banghi2.AnhMot = danhsach[danhsach.Length - 1].Anh;
                        banghi2.MaHangHoaHai = null;
                        banghi2.TenHangHoaHai = null;
                        banghi2.AnhHai = null;
                        banghi2.MaHangHoaBa = null;
                        banghi2.TenHangHoaBa = null;
                        banghi2.AnhBa = null;
                        banghi2.MaHangHoaBon = null;
                        banghi2.TenHangHoaBon = null;
                        banghi2.AnhBon = null;
                        banghi2.MaHangHoaNam = null;
                        banghi2.TenHangHoaNam = null;
                        banghi2.AnhNam = null;
                        list.Add(banghi2);
                    }
                    else
                    {
                        if (danhsach.Length == 2)
                        {
                            Entities.Barcode_A4 banghi3 = new Entities.Barcode_A4();
                            banghi3.MaHangHoaMot = danhsach[danhsach.Length - 1].MaHangHoa;
                            banghi3.TenHangHoaMot = danhsach[danhsach.Length - 1].TenHangHoa;
                            banghi3.AnhMot = danhsach[danhsach.Length - 1].Anh;
                            banghi3.MaHangHoaHai = danhsach[danhsach.Length - 2].MaHangHoa;
                            banghi3.TenHangHoaHai = danhsach[danhsach.Length - 2].TenHangHoa;
                            banghi3.AnhHai = danhsach[danhsach.Length - 2].Anh;
                            banghi3.MaHangHoaBa = null;
                            banghi3.TenHangHoaBa = null;
                            banghi3.AnhBa = null;
                            banghi3.MaHangHoaBon = null;
                            banghi3.TenHangHoaBon = null;
                            banghi3.AnhBon = null;
                            banghi3.MaHangHoaNam = null;
                            banghi3.TenHangHoaNam = null;
                            banghi3.AnhNam = null;
                            list.Add(banghi3);
                        }
                        else
                        {
                            if (danhsach.Length == 3)
                            {
                                Entities.Barcode_A4 banghi4 = new Entities.Barcode_A4();
                                banghi4.MaHangHoaMot = danhsach[danhsach.Length - 1].MaHangHoa;
                                banghi4.TenHangHoaMot = danhsach[danhsach.Length - 1].TenHangHoa;
                                banghi4.AnhMot = danhsach[danhsach.Length - 1].Anh;
                                banghi4.MaHangHoaHai = danhsach[danhsach.Length - 2].MaHangHoa;
                                banghi4.TenHangHoaHai = danhsach[danhsach.Length - 2].TenHangHoa;
                                banghi4.AnhHai = danhsach[danhsach.Length - 2].Anh;
                                banghi4.MaHangHoaBa = danhsach[danhsach.Length - 3].MaHangHoa;
                                banghi4.TenHangHoaBa = danhsach[danhsach.Length - 3].TenHangHoa;
                                banghi4.AnhBa = danhsach[danhsach.Length - 3].Anh;
                                banghi4.MaHangHoaBon = null;
                                banghi4.TenHangHoaBon = null;
                                banghi4.AnhBon = null;
                                banghi4.MaHangHoaNam = null;
                                banghi4.TenHangHoaNam = null;
                                banghi4.AnhNam = null;
                                list.Add(banghi4);
                            }
                            else
                            {
                                if (danhsach.Length == 4)
                                {
                                    Entities.Barcode_A4 banghi5 = new Entities.Barcode_A4();
                                    banghi5.MaHangHoaMot = danhsach[danhsach.Length - 1].MaHangHoa;
                                    banghi5.TenHangHoaMot = danhsach[danhsach.Length - 1].TenHangHoa;
                                    banghi5.AnhMot = danhsach[danhsach.Length - 1].Anh;
                                    banghi5.MaHangHoaHai = danhsach[danhsach.Length - 2].MaHangHoa;
                                    banghi5.TenHangHoaHai = danhsach[danhsach.Length - 2].TenHangHoa;
                                    banghi5.AnhHai = danhsach[danhsach.Length - 2].Anh;
                                    banghi5.MaHangHoaBa = danhsach[danhsach.Length - 3].MaHangHoa;
                                    banghi5.TenHangHoaBa = danhsach[danhsach.Length - 3].TenHangHoa;
                                    banghi5.AnhBa = danhsach[danhsach.Length - 3].Anh;
                                    banghi5.MaHangHoaBon = danhsach[danhsach.Length - 4].MaHangHoa;
                                    banghi5.TenHangHoaBon = danhsach[danhsach.Length - 4].TenHangHoa;
                                    banghi5.AnhBon = danhsach[danhsach.Length - 4].Anh;
                                    banghi5.MaHangHoaNam = null;
                                    banghi5.TenHangHoaNam = null;
                                    banghi5.AnhNam = null;
                                    list.Add(banghi5);
                                }
                                else
                                {
                                    tra = null;
                                }
                            }
                        }
                    }

                }
                int n = list.Count;
                if (n == 0) { tra = null; }
                tra = new Entities.Barcode_A4[n];
                for (int i = 0; i < n; i++)
                {
                    tra[i] = (Entities.Barcode_A4)list[i];
                }
            }
            catch (Exception ex)
            { string s = ex.Message; tra = null; danhsach = null; }
            return tra;
        }
        #endregion

        #region in
        private void Print()
        {
            try
            {
                PrintDocument doc = new PrintDocument();
                doc.PrintPage += this.PrintData;
                PrintDialog dlgSettings = new PrintDialog();
                dlgSettings.Document = doc;
                if (dlgSettings.ShowDialog() == DialogResult.OK)
                {
                    doc.Print();
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private string ten;
        public void PrintData(object sender, PrintPageEventArgs e)
        {
            try
            {
                Font font = new Font("Arial", 10);
                float x = e.MarginBounds.Left;
                float y = e.MarginBounds.Top;
                float lineHeight = font.GetHeight(e.Graphics);
                e.Graphics.DrawString(ten, font, Brushes.Black, x - 20, 0);
                e.Graphics.DrawImage(img, 0, 20);
                e.Graphics.Dispose();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        #region sự kiện form,addrow
        private Entities.InMaVach[] getdata(DataGridView dgv, string hanhdong, int i)
        {
            ArrayList arr = new ArrayList();
            Entities.InMaVach[] list = null;
            try
            {
                if (hanhdong == "BanGhi")
                {
                    if (i >= 0)
                    {
                        Entities.InMaVach row = new Entities.InMaVach();
                        string ma = dgv.Rows[i].Cells[2].Value.ToString();
                        string ten = dgv.Rows[i].Cells[3].Value.ToString();
                        txtData.Text = ma;
                        txtTen.Text = ten;
                        row.MaHangHoa = ma;
                        row.TenHangHoa = ten;
                        this.ten = ten;
                        VeMaVach(ma);
                        System.IO.MemoryStream ms = new System.IO.MemoryStream();
                        barcode.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        Byte[] manganh = new Byte[ms.Length];
                        ms.Position = 0;
                        ms.Read(manganh, 0, Convert.ToInt32(ms.Length));
                        row.Anh = manganh;
                        arr.Add(row);
                        ma = null;
                        ten = null;
                        img = null;
                    }
                }
                if (hanhdong == "Bang")
                {
                    for (int j = 0; j < dgv.RowCount; j++)
                    {
                        if (dgv[0, j].Value.ToString() == "True")
                        {
                            img = null;
                            Entities.InMaVach row = new Entities.InMaVach();
                            string ma = dgv.Rows[j].Cells[2].Value.ToString();
                            string ten = dgv.Rows[j].Cells[3].Value.ToString();
                            txtData.Text = ma;
                            txtTen.Text = ten;
                            row.MaHangHoa = ma;
                            row.TenHangHoa = ten;
                            VeMaVach(ma);
                            System.IO.MemoryStream ms = new System.IO.MemoryStream();
                            barcode.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                            Byte[] manganh = new Byte[ms.Length];
                            ms.Position = 0;
                            ms.Read(manganh, 0, Convert.ToInt32(ms.Length));
                            row.Anh = manganh;
                            arr.Add(row);
                            ma = null;
                            ten = null;
                            img = null;
                        }
                        else
                        { continue; }
                    }
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.InMaVach[n];
                for (int x = 0; x < n; x++)
                {
                    list[x] = (Entities.InMaVach)arr[x];
                }
            }
            catch (Exception ex)
            { string s = ex.Message; list = null; }
            return list;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBox1.Checked == true)
                {
                    txtWidth.ReadOnly = true;
                    txtHeight.ReadOnly = true;
                    cbxLoaigiay.SelectedIndex = 0;
                    cbxLoaigiay.Enabled = true;
                }
                if (checkBox1.Checked == false)
                {
                    txtWidth.ReadOnly = false;
                    txtHeight.ReadOnly = false;
                    cbxLoaigiay.SelectedIndex = 0;
                    cbxLoaigiay.Enabled = false;
                }

                fix();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private Entities.ThongTinMaVach[] search;
        private void CheckData(Boolean c)
        {
            try
            {
                ArrayList list = new ArrayList();
                Entities.ThongTinMaVach[] check = null;
                for (int i = 0; i < search.Length; i++)
                {
                    Entities.ThongTinMaVach banghi = new Entities.ThongTinMaVach();
                    banghi.ChonIn = c;
                    banghi.HanhDong = search[i].HanhDong;
                    banghi.MaHangHoa = search[i].MaHangHoa;
                    banghi.TenHangHoa = search[i].TenHangHoa;
                    banghi.GiaNhap = search[i].GiaNhap;
                    banghi.GiaBanBuon = search[i].GiaBanBuon;
                    banghi.GiaBanLe = search[i].GiaBanLe;
                    list.Add(banghi);
                }
                int n = list.Count;
                if (n == 0) { check = null; }
                check = new Entities.ThongTinMaVach[n];
                for (int i = 0; i < n; i++)
                {
                    check[i] = (Entities.ThongTinMaVach)list[i];
                }
                search = check;
                dgvHangHoa.DataSource = search;
                FixDatagridview();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void cbxLoaigiay_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                txtWidth.ReadOnly = true;
                txtHeight.ReadOnly = true;
                if (cbxLoaigiay.SelectedItem.ToString() == "Loại A5")
                {
                    soCot = 2;
                    txtWidth.Text = "350";
                    txtHeight.Text = "220";
                }
                if (cbxLoaigiay.SelectedItem.ToString() == "Loại 110")
                {
                    soCot = 3;
                    txtWidth.Text = "350";
                    txtHeight.Text = "180";
                }
                if (cbxLoaigiay.SelectedItem.ToString() == "Loại A4")
                {
                    soCot = 5;
                    txtWidth.Text = "350";
                    txtHeight.Text = "220";
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void txtWidth_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtHeight_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtMaHangHoa_Click(object sender, EventArgs e)
        {
            txtMaHangHoa.Text = "";
            txtTenHangHoa.Text = "";
            txtSoLuongIn.Text = "1";
        }

        private Entities.ThongTinMaVach RowsData()
        {
            Entities.ThongTinMaVach r = new Entities.ThongTinMaVach();
            try
            {
                if (txtMaHangHoa.Text == string.Empty || txtMaHangHoa.Text == "F4 - Tra cứu")
                {
                    MessageBox.Show("Bạn phải nhập mã hàng hóa");
                    txtMaHangHoa.Focus();
                    return null;
                }
                if (txtTenHangHoa.Text == string.Empty)
                {
                    MessageBox.Show("Bạn phải nhập tên hàng hóa");
                    txtMaHangHoa.Text = "";
                    txtTenHangHoa.Text = "";
                    txtSoLuongIn.Text = "1";
                    txtMaHangHoa.Focus();
                    return null;
                }
                try
                {
                    if (int.Parse("0" + txtSoLuongIn.Text) <= 0)
                    {
                        MessageBox.Show("Số lượng in nhập không đúng");
                        txtSoLuongIn.Text = "1";
                        txtSoLuongIn.Focus();
                        return null;
                    }
                }
                catch
                {
                    MessageBox.Show("Số lượng in nhập không đúng");
                    txtSoLuongIn.Text = "1";
                    txtSoLuongIn.Focus();
                    return null;
                }
                if (int.Parse(txtSoLuongIn.Text) > 201)
                {
                    MessageBox.Show("Số lượng in tối đa là: 200");
                    txtSoLuongIn.Text = "200";
                    txtSoLuongIn.Focus();
                    return null;
                }
                r.MaHangHoa = txtMaHangHoa.Text;
                r.TenHangHoa = txtTenHangHoa.Text;
                r.GhiChu = txtSoLuongIn.Text;
            }
            catch (Exception)
            {
                r = null;
            }
            return r;
        }

        private void AddRow(Entities.ThongTinMaVach rows)
        {
            ArrayList li = new ArrayList();
            try
            {
                Boolean kiemtrasoluong = false;
                if (listview == null)
                {
                    li.Add(rows);
                }
                else
                {
                    Boolean kt = false;
                    for (int i = 0; i < listview.Length; i++)
                    {
                        if (listview[i].MaHangHoa == rows.MaHangHoa)
                        {
                            kt = true;
                            int sl = int.Parse(listview[i].GhiChu) + int.Parse(rows.GhiChu);
                            if (sl > 201)
                            {
                                MessageBox.Show("Số lượng in tối đa là: 200");
                                txtSoLuongIn.Focus();
                                kiemtrasoluong = true;
                                break;
                            }
                            rows.GhiChu = (sl).ToString();
                            li.Add(rows);
                        }
                        else
                        {
                            li.Add(listview[i]);
                        }
                    }
                    if (kt == false)
                    {
                        li.Add(rows);
                    }
                }
                if (kiemtrasoluong == false)
                {
                    int x = li.Count;
                    if (x <= 0)
                    {
                        listview = new Entities.ThongTinMaVach[0];
                    }
                    else
                    {
                        listview = new Entities.ThongTinMaVach[x];
                        for (int i = 0; i < x; i++)
                        {
                            listview[i] = (Entities.ThongTinMaVach)li[i];
                        }
                    }
                    dgvHangHoa.DataSource = listview;
                    FixDatagridview();
                }
            }
            catch (Exception)
            {
            }
        }

        public static Entities.ThongTinMaVach timhanghoa;

        private void txtMaHangHoa_KeyDown(object sender, KeyEventArgs e)
        {
             try
            {
                switch (e.KeyCode)
                {
                    case Keys.Enter:
                        this.GetHangHoa(txtMaHangHoa.Text);
                        txtSoLuongIn.Focus();
                        break;
                    case Keys.F4:
                        TimKiem.frmHangHoa frm = new TimKiem.frmHangHoa();
                        frm.ShowDialog();
                        if(TimKiem.frmHangHoa.selectedItem != null)
                        {
                            txtMaHangHoa.Text = TimKiem.frmHangHoa.selectedItem.MaHangHoa;
                            txtTenHangHoa.Text = TimKiem.frmHangHoa.selectedItem.TenHangHoa;
                            txtSoLuongIn.Text = "1";
                            txtSoLuongIn.Focus();
                        }
                        //if (timhanghoa != null)
                        //{
                        //    txtMaHangHoa.Text = timhanghoa.MaHangHoa;
                        //    txtTenHangHoa.Text = timhanghoa.TenHangHoa;
                        //    txtSoLuongIn.Text = "1";
                        //    txtSoLuongIn.Focus();
                        //}
                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {
            }
        
        }

        private void txtMaHangHoa_TextChanged(object sender, EventArgs e)
        {
            txtTenHangHoa.Text = "";
            txtSoLuongIn.Text = "1";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            this.AddRow(this.RowsData());
            txtMaHangHoa.Text = "";
            txtTenHangHoa.Text = "";
            txtSoLuongIn.Text = "1";
            txtMaHangHoa.Focus();
        }

        private void txtSoLuongIn_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.AddRow(this.RowsData());
                txtMaHangHoa.Text = "";
                txtTenHangHoa.Text = "";
                txtSoLuongIn.Text = "1";
                txtMaHangHoa.Focus();
            }
        }

        private void dgvHangHoa_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (listview != null)
                {
                    this.MaHangHoa = dgvHangHoa.Rows[e.RowIndex].Cells["MaHangHoa"].Value.ToString();
                }
            }
            catch (Exception)
            {
            }
        }
        #endregion

    }
}
